package cn.youyeku.zsxh.ui.home;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.AppConfig;
import cn.youyeku.zsxh.Utils.CircleImageView;
import cn.youyeku.zsxh.Utils.ImageUtils;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.adapter.SimpleAdapter;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.bean.MsgList;
import cn.youyeku.zsxh.db.YeDBConnect;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.ui.activity.ABS.HandleActivity;

/**
 * Created by youye on 17-4-2.
 */

public class MsgListActivity extends HandleActivity {

    YeDBConnect ye;
    Cursor cursor;
    String sql = " SELECT\n" +
            " DISTINCT\n" +
            " sender_id,\n" +
            " message_content,\n" +
            " send_time,\n" +
            " nick_name,\n" +
            " message_status,\n" +
            " message_type \n" +
            " FROM\n" +
            " `%s`\n" +
            " GROUP BY sender_id\n" +
            " ORDER BY send_time DESC";

    private List<MsgList> msgList =new ArrayList<>();
    protected SimpleAdapter mAdapter;
    private RecyclerView mRecyCommon;

    LinearLayoutManager layoutManager;

    @Override
    protected void onChange() {

    }

    @Override
    protected void onError() {

    }

    @Override
    protected void onSuccess() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_recycle);
        initView();
        initActionBar();

        ye = YeDBConnect.getInstance(getApplicationContext(), 'w');
        cursor = ye.executeFetchData(String.format(sql, AppConfig.CONF_USER_ID));
        while (cursor.moveToNext()) {
            int sender_id = cursor.getInt(0);
            String message = cursor.getString(1);
            String nick_name = cursor.getString(3);
            msgList.add(new MsgList(sender_id, message, nick_name));
            LogUtils.w("什么都没" + sender_id + "  " + nick_name+message);
        }

        setLayoutV_H(LinearLayoutManager.VERTICAL);
        mAdapter = new SimpleAdapter<MsgList>(R.layout.item_msg) {

            @Override
            protected void setmOnItemClick(View item, final MsgList data) {
                item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UIControl.showPrivateMessage(getApplicationContext(),data.getSender_id(),data.getNick_name());
                    }
                });

            }

            @Override
            protected void onBindData(ListHolder listHolder, View item, MsgList data) {

                CircleImageView img = listHolder.getChildView(R.id.recycler_item_image);
                TextView nickName = listHolder.getChildView(R.id.recycler_item_nick);
                TextView history = listHolder.getChildView(R.id.recycler_item_text);

                ImageUtils.GlideCircleImg(getApplicationContext(), img, XhApi.getFace(data.getSender_id()));
                nickName.setText(data.getNick_name());
                history.setText(data.getMessage());


            }
        };

        mRecyCommon.setAdapter(mAdapter);
        mAdapter.add(msgList);


    }

    /**
     * @param type 设置水平方向还是垂直布局
     */
    public void setLayoutV_H(int type) {
        layoutManager = new LinearLayoutManager(getApplicationContext());
        layoutManager.setOrientation(type);
        mRecyCommon.setLayoutManager(layoutManager);
    }

    private void initView() {
        mRecyCommon = (RecyclerView) findViewById(R.id.recy_common);
    }
}
